﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using UFIDA.U9.Cust.YYD.WebApi.Dtos;
using UFIDA.U9.Cust.YYD.WebApi.Dtos.RequestDtos;
using UFIDA.U9.Cust.YYD.WebApi.Dtos.ReturnDtos;
using UFIDA.U9.Cust.YYD.WebApi.Public;
using UFIDA.UBF.SystemManage;
using UFIDA.UBF.SystemManage.Agent;
using UFSoft.UBF.AopFrame.Lock;
using UFSoft.UBF.Util.DataAccess;

namespace UFIDA.U9.Cust.YYD.WebApi.Controllers
{
    public class PDAController : ApiController
    {
        [HttpPost]
        public ResultDto<IDCodeName> U9Login(RequestPara<LoginPara> paras)
        {
            using (BPForEngine bp1 = new BPForEngine())
            {
                PublicMethod.WriteContext(paras.Context, JsonConvert.SerializeObject(paras), this.GetType().Name);

                var data = paras.Data;
                //接口逻辑
                var result = new ResultDto<IDCodeName>();
                if (string.IsNullOrEmpty(data.UserName) || string.IsNullOrEmpty(data.Password))
                {
                    result.Code = 0;
                    result.Msg = "用户名或密码不能为空";
                    return result;
                }
                //账号密码验证
                string PW = Crypto.U9Encrypt(data.Password);
                string pwsql = $"select Name from Base_User WHERE code='{data.UserName}' and password=upper(substring(sys.fn_VarBinToHexStr(hashbytes('MD5', CAST('{PW}' as varchar(50)) + convert(char(36),base_user.salt))),3,32))";
                DataAccessor.RunSQL(DataAccessor.GetConn(), pwsql, null, out DataSet pwDS);
                if (pwDS.Tables[0].Rows.Count == 0)
                {
                    result.Code = 0;
                    result.Msg = "用户名或密码错误";
                    return result;
                }
                else
                {
                    result.Data = new IDCodeName
                    {
                        ID = Crypto.AESEncrypt(data.UserName),
                        Name = pwDS.Tables[0].Rows[0]["Name"].ToString()
                    };
                    return result;
                }
            }
        }
        [HttpGet]
        public ResultDto<List<IDCodeName>> GetEntList()
        {
            ResultDto<List<IDCodeName>> result = new ResultDto<List<IDCodeName>>();
            var entList = new List<Enterprise>();
            List<IDCodeName> codeNames = new List<IDCodeName>();
            var mainc = new LicenseInfo().GetMainControlNew();
            if (mainc != null && mainc.AllSites != null && mainc.AllSites.Count > 0)
            {
                var site = mainc.AllSites[0];
                if (site != null && site.Enterprises != null && site.Enterprises.Count > 0)
                {
                    foreach (Enterprise item in site.Enterprises)
                    {
                        if (item.IsCanUse)

                        {
                            entList.Add(item);
                            codeNames.Add(new IDCodeName
                            {
                                Code = item.Code,
                                Name = item.Name
                            });
                        }
                    }
                }
            }
            result.Data = codeNames;
            return result;
        }
    }
}
